package com.godpaper.the2tigers.util
{
	import com.godpaper.the2tigers.model.BoardModel;
	import com.godpaper.the2tigers.views.components.PieceButton;
	
	import mx.collections.ArrayCollection;
	
	
	public class PositionTransactUtil
	{
		/*public static function transactIndex2Array(target:int):Array
		{
			return new Array(int(target/4),target%4);
		}*/
		/**
		 * 
		 * @param position
		 * @return gasketButton's position to the index of gameGraph.
		 * 
		 */		
		public static function transactArray2Index(position:Array):int//gasket button position to index.
		{
			var result:int=-1;
			result = position[0]+position[1]*5;
			return result;
		}
		/**
		 * 
		 * @param walkableGBs
		 * @return whether in walkable existed at least of 3PiecesButtons linked as a line(not a ring).
		 * 
		 */		
		public static function doesWalkable2positionsIsLinkable(original:Array,destination:Array):Boolean
		{
			var isSideBy:Boolean = false;
			//check this two position whether if sideby.
			//is left?
			if(original[0]-1==destination[0])
			{
				isSideBy = true;
			}
			//is lowerLeft?
			if(original[0]-1==destination[0] && original[1]+1==destination[1])
			{
				isSideBy = true;
			}
			//is bottom?
			if(original[1]-1==destination[1])
			{
				isSideBy = true;
			}
			//is lowerRight?
			if(original[0]+1==destination[0] && original[1]+1==destination[1])
			{
				isSideBy = true;
			}
			//is right?
			if(original[0]+1==destination[0])
			{
				isSideBy = true;
			}
			//is upperRight?
			if(original[0]+1==destination[0] && original[1]-1==destination[1])
			{
				isSideBy = true;
			}
			//is top?
			if(original[1]+1==destination[1])
			{
				isSideBy = true;
			}
			//is upperLeft?
			if(original[0]-1==destination[0] && original[1]-1==destination[1])
			{
				isSideBy = true;
			}
			
			 return isSideBy;
		}
		/**
		 * 
		 * @param original
		 * @param destination
		 * @return dogs in the way.
		 * 
		 */		
		public static function findDogsInTheWay(original:Array,destination:Array):ArrayCollection
		{
			//is left's left?
			if(original[0]-2 == destination[0])
			{
				trace( "left's left:",BoardModel.getInstance().gamePosition.gett(original[0]-1,original[1]) as ArrayCollection );
				return BoardModel.getInstance().gamePosition.gett(original[0]-1,original[1]) as ArrayCollection;
			}			
			//is lowerLeft's lowerLeft?
			if(original[0]-2==destination[0] && original[1]+2==destination[1])
			{
				trace( "lowerLeft's lowerLeft:",BoardModel.getInstance().gamePosition.gett(original[0]-1,original[1]+1)  as ArrayCollection );
				return BoardModel.getInstance().gamePosition.gett(original[0]-1,original[1]+1) as ArrayCollection;
			}
			//is bottom's bottom?
			if(original[1]+2 == destination[1])
			{
				trace( "bottom's bottom:",BoardModel.getInstance().gamePosition.gett(original[0],original[1]+1)  as ArrayCollection);
				return BoardModel.getInstance().gamePosition.gett(original[0],original[1]+1) as ArrayCollection;
			}	
			//is lowerRight's lowerRight?
			if(original[0]+2==destination[0] && original[1]+2==destination[1])
			{
				trace( "lowerRight's lowerRight:",BoardModel.getInstance().gamePosition.gett(original[0]+1,original[1]+1)  as ArrayCollection);
				return BoardModel.getInstance().gamePosition.gett(original[0]+1,original[1]+1) as ArrayCollection;
			}
			//is right's right?
			if(original[0]+2 == destination[0])
			{
				trace( "right's right:",BoardModel.getInstance().gamePosition.gett(original[0]+1,original[1])  as ArrayCollection);
				return BoardModel.getInstance().gamePosition.gett(original[0]+1,original[1]) as ArrayCollection;
			}	
			//is upperRight's upperRight?
			if(original[0]+2==destination[0] && original[1]-2==destination[1])
			{
				trace( "upperRight's upperRight:",BoardModel.getInstance().gamePosition.gett(original[0]+1,original[1]-1)  as ArrayCollection);
				return BoardModel.getInstance().gamePosition.gett(original[0]+1,original[1]-1) as ArrayCollection;
			}
			//is top's top?
			if(original[1]-2 == destination[1])
			{
				trace( "top's top:",BoardModel.getInstance().gamePosition.gett(original[0],original[1]-1)  as ArrayCollection);
				return BoardModel.getInstance().gamePosition.gett(original[0],original[1]-1) as ArrayCollection;
			}	
			//is upperLeft's upperLeft?
			if(original[0]-2==destination[0] && original[1]-2==destination[1])
			{
				trace( "upperLeft's upperLeft:",BoardModel.getInstance().gamePosition.gett(original[0]-1,original[1]-1)  as ArrayCollection);
				return BoardModel.getInstance().gamePosition.gett(original[0]-1,original[1]-1) as ArrayCollection;
			}
			return null;
		}
	}
}